package ar.com.photo_admin.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import ar.com.photo_admin.Constants;
import ar.com.photo_admin.R;
import ar.com.photo_admin.domain.AlbumLightWeight;
import ar.com.photo_admin.domain.Event;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.PauseOnScrollListener;

public class AlbumListActivity extends ListActivity {
	
	private List<AlbumLightWeight> albumns = new ArrayList<AlbumLightWeight>();
	
	private ArrayAdapter<AlbumLightWeight> adapter;
	
	private DisplayImageOptions options;
	
	private ImageLoader imageLoader = ImageLoader.getInstance();
	
	private boolean instanceStateSaved;
	
	private Event event;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.albums);
		
		/*
		 * Checks if there is saved instance state.
		 */
		event = (Event) (savedInstanceState != null ?  
				savedInstanceState.getSerializable(Constants.EVENT) :
				getIntent().getExtras().getSerializable(Constants.EVENT));
		
		if (event.getAlbums() != null)
			albumns = event.getAlbums();
			
		setTitle(getString(R.string.albums_from) + " " + event.getName());
		
		options = new DisplayImageOptions.Builder()
			.showStubImage(R.drawable.ic_stub)
			.showImageForEmptyUri(R.drawable.ic_empty)
			.cacheInMemory()
			.cacheOnDisc()
			.build();
		
		this.adapter = (new ArrayAdapter<AlbumLightWeight>(this,
				R.layout.item_list_image, albumns) {
			
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				
				View row = convertView;

				if (row == null) {
					LayoutInflater inflater = getLayoutInflater();
					row = inflater.inflate(R.layout.item_list_image, parent, false);
				}
				
				final AlbumLightWeight album = getItem(position);
				
				TextView name = (TextView) row.findViewById(R.id.text);
				name.setText(album.getName());
				 
				ImageView image = (ImageView) row.findViewById(R.id.image);
				imageLoader.displayImage(album.getCover().getThumbnailUrl(), image, options);
			    
				return row;
			}
		});
		
		this.getListView().setOnScrollListener(new PauseOnScrollListener(false, true));

		setListAdapter(adapter);
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		
		final AlbumLightWeight album = adapter.getItem(position);
		Toast.makeText(this, album.getName(), Toast.LENGTH_SHORT).show();
		
		Intent intent = new Intent(this, PhotoGridActivity.class);
		intent.putExtra(Constants.TOKEN, event.getToken());
		intent.putExtra(Constants.ALBUM_ID, album.getId());
		
		startActivity(intent);
	}
	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		
		instanceStateSaved = true;
		outState.putSerializable(Constants.EVENT, event);
	}

	@Override
	protected void onDestroy() {
		if (!instanceStateSaved) {
			imageLoader.stop();
		}
		super.onDestroy();
	}
}